use super::ReadFileArgs;
use super::ToolResult;
use crate::models::ProjectConfig;
use crate::utils::file_ops;
use anyhow::{Context, Result};

pub fn read_file(args: &ReadFileArgs, context: &ProjectConfig) -> Result<ToolResult> {

    let full_path = file_ops::join_paths(&context.path.to_string_lossy(), &args.file_path)
        .with_context(|| {
            format!(
                "Failed to join paths: {} and {}",
                context.path.to_string_lossy(),
                args.file_path
            )
        })?;

    println!("read_file:{}", full_path.to_string_lossy());
    let content = file_ops::read_file_to_string(&full_path)
        .with_context(|| format!("Failed to read file at {}", full_path.display()))?;
    println!("read_file successfully");

    Ok(ToolResult::new(
        args.call_id.clone().unwrap_or_default(),
        args.name.clone().unwrap_or_default(),
        &content,
    ))
}
